<?php

Class page_model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function get_pages($from) {

        $this->db->limit(1,$from); // устанавливаем количество записей на странице
        $this->db->order_by("id", "asc"); // устанавливаем порядок следования записей
        $query = $this->db->get('page');

        return $query->result_array();
    }

    public function insert_page($title, $link, $text, $category) {

        $data = array(
            'title' => url_title($title, 'dash', TRUE),
            'link' => $link,
            'text' => $text,
            'cat'=>$category
        );

        $this->db->insert('page', $data);
    }

    public function get_pages_id($id) {

        $query = $this->db->get_where('page', array('id' => $id), 1);

        return $query->row_array();
    }

    public function update_page($id, $title, $link, $text, $category) {

        if (isset($id) && !empty($id) && $id !== 0) {

            $data = array(
                'title' => $title,
                'link' => url_title($link, 'dash', TRUE),
                'text' => $text,
                'cat'=>(int)$category
            );

            $this->db->where('id', (int) $id);
            $this->db->update('page', $data);

            return true;
        }

        return false;
    }

    public function add_photo($filename, $file_content) {

        $title = $this->input->post('title');

        $caption = $this->input->post('caption');

        $post_id = $this->input->post('post_id');

        // clean and make the fiename unique

        $disallow = array(' ', '`', '"', '\'', '\\', '/', '<', '>', '%', '$', ':', '*');

        $newname = str_replace($disallow, '', $filename);

        $newname = time() . '-' . $newname;



        //move the file to it's final destination

        $location = "./uploads/$newname";

        move_uploaded_file($file_content, $location);


/*
        $data = array(
            'title' => $title,
            'caption' => $caption,
            'post_id' => $post_id,
            'filename' => $newname,
            'path' => base_url() . 'uploads/' . $newname
        );
*/
// update the database table

    //  $this->db->insert('blog_pics', $data);



//formulate the response (html) to insert in the editor instance, using title as image alt attribute

        $imageurl = base_url().'uploads/' . $newname;

        $response = '<img style="display: block; margin-left: auto; margin-right: auto;" src="'.$imageurl.'" alt="'.$title.'"/><p style="text-align: center;"><em>'.$caption.'</em></p>';

       // ..

        //I also resize the image and create a thumbnail in this bit, removed to keep this code short

        return $response;
    }
    
    public function get_cats(){
        
        $query = $this->db->get('category');

        return $query->result_array();
        
    }
    
    public function get_cats_id($id){
        
        $query = $this->db->get_where('category', array('id' => (int)$id), 1);

        return $query->row_array();
        
        
    }
    
    public function edit_cat($id, $link, $name){
        
         if (isset($id) && !empty($id) && $id !== 0) {

            $data = array(
                'link' => $link,
                'name' => $name
            );

            $this->db->where('id', (int) $id);
            $this->db->update('category', $data);

            return true;
        }

        return false;
    }
    
    public function insert_cat($link, $name){
        
         $data = array(
            'link' => $link,
            'name' => $name
        );

        $this->db->insert('category', $data);
        
    }
    
    public function delcat($id){
        
        if ($id!==NULL && $id!==0){
        
        $this->db->delete('category', array('id' => (int)$id)); 
        
        return "Удалено!";
        }
        
        return "Ошибка!";
    }


public function delpage($id){
        
        if ($id!==NULL && $id!==0){
        
        $this->db->delete('page', array('id' => (int)$id)); 
        
        return "Удалено!";
        }
        
        return "Ошибка!";
    }
    
       public function get_all_category() {

        $query = $this->db->get('category');

        $result =  $query->result_array();
        
        $options = array();
        foreach ($result as $row){
         $options[$row['id']] = $row['name'];   
            
        }
        
        return $options;
        
        
    }

}
?>
